<template>
  <!-- gutter: 行内各列之间的水平间距-->
  <el-row class="home" :gutter="20">
    <!-- 左侧 -->
    <el-col :span="8" style="margin-top: 20px">
      <!-- 上 -->
      <el-card shadow="hover" class="user-table">
        <div class="user">
          <img  src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png" class="user" />
          <div class="user-info">
            <p class="user-info-admin">Admin</p>
            <p class="user-info-p">超级管理员</p>
          </div>
        </div>
        <div class="login-info">
          <p>上次登录时间：<span>2020-06-06</span></p>
          <p>上次登录地点：<span>北京</span></p>
        </div>
      </el-card>
      <!-- 中 -->
      <el-card shadow="hover" class="user-table">
        <el-table :data="tableData">
          <!-- :prop：用于指定该列要显示的数据字段，key 对应 tableData 中每个对象的属性名 -->
          <!-- :label：用于指定该列的表头标签。val 对应 tableLabel 中每个属性的值 -->
          <el-table-column
            v-for="(val, key) in tableLabel"
            :key="key"
            :prop="key"
            :label="val"
          >
          </el-table-column>
        </el-table>
      </el-card>
    </el-col>
    <!-- 右侧 -->
    <el-col :span="16" style="margin-top: 20px">
      <div class="num">
        <el-card
          :body-style="{ display: 'flex', padding: 0 }"
          v-for="item in countData"
          :key="item.name"
        >
          <component
            :is="item.icon"
            class="icons"
            :style="{ background: item.color }"
          ></component>
          <div class="detail">
            <p class="num">￥{{ item.value }}</p>
            <p class="txt">￥{{ item.name }}</p>
          </div>
        </el-card>
      </div>
      <!-- 图表 -->
      <el-card class="top-echart">
        <div ref="echart" style="height: 230px"></div>
      </el-card>
      <div class="graph">
        <el-card>
          <div ref="userEchart" style="height: 200px"></div>
        </el-card>
        <el-card>
          <div ref="videoEchart" style="height: 200px"></div>
        </el-card>
      </div>
    </el-col>
  </el-row>
</template>

<script setup>
import {computed,reactive,ref,onMounted,getCurrentInstance} from "vue";
import { useRouter, useRoute } from "vue-router";
const { proxy } = getCurrentInstance();
import * as echarts from "echarts";
const tableData = ref([
  {
              name: "oppo",
              todayBuy: 500,
              monthBuy: 3500,
              totalBuy: 22000,
            },
            {
              name: "vivo",
              todayBuy: 300,
              monthBuy: 2200,
              totalBuy: 24000,
            },
            {
              name: "苹果",
              todayBuy: 800,
              monthBuy: 4500,
              totalBuy: 65000,
            },
            {
              name: "小米",
              todayBuy: 1200,
              monthBuy: 6500,
              totalBuy: 45000,
            },
            {
              name: "三星",
              todayBuy: 300,
              monthBuy: 2000,
              totalBuy: 34000,
            },
            {
              name: "魅族",
              todayBuy: 350,
              monthBuy: 3000,
              totalBuy: 22000,
            }
]);
const countData = ref([
  {
            name: "今日支付订单",
            value: 1234,
            icon: "SuccessFilled",
            color: "#2ec7c9",
          },
          {
            name: "今日收藏订单",
            value: 210,
            icon: "StarFilled",
            color: "#ffb980",
          },
          {
            name: "今日未支付订单",
            value: 1234,
            icon: "GoodsFilled",
            color: "#5ab1ef",
          },
          {
            name: "本月支付订单",
            value: 1234,
            icon: "SuccessFilled",
            color: "#2ec7c9",
          },
          {
            name: "本月收藏订单",
            value: 210,
            icon: "StarFilled",
            color: "#ffb980",
          },
          {
            name: "本月未支付订单",
            value: 1234,
            icon: "GoodsFilled",
            color: "#5ab1ef",
          },
]);
const observe = ref(null);
const tableLabel = ref({
  name: "课程",
  todayBuy: "今日购买",
  monthBuy: "本月购买",
  totalBuy: "总购买",
});
// 直接定义图表数据
const chartData = {
  orderData: {
    date: [
      "2019-10-01",
      "2019-10-02",
      "2019-10-03",
      "2019-10-04",
      "2019-10-05",
      "2019-10-06",
      "2019-10-07",
    ],
    data: [
      { 苹果: 3839, 小米: 1423, 华为: 4965, oppo: 3334, vivo: 2820, 一加: 4751 },
      { 苹果: 3560, 小米: 2099, 华为: 3192, oppo: 4210, vivo: 1283, 一加: 1613 },
      { 苹果: 1864, 小米: 4598, 华为: 4202, oppo: 4377, vivo: 4123, 一加: 4750 },
      { 苹果: 2634, 小米: 1458, 华为: 4155, oppo: 2847, vivo: 2551, 一加: 1733 },
      { 苹果: 3622, 小米: 3990, 华为: 2860, oppo: 3870, vivo: 1852, 一加: 1712 },
      { 苹果: 2004, 小米: 1864, 华为: 1395, oppo: 1315, vivo: 4051, 一加: 2293 },
      { 苹果: 3797, 小米: 3936, 华为: 3642, oppo: 4408, vivo: 3374, 一加: 3874 }
    ]
  },
  videoData: [
    { name: "小米", value: 2999 },
    { name: "苹果", value: 5999 },
    { name: "vivo", value: 1500 },
    { name: "oppo", value: 1999 },
    { name: "魅族", value: 2200 },
    { name: "三星", value: 4500 }
  ],
  userData: [
    { date: "周一", new: 5, active: 200 },
    { date: "周二", new: 10, active: 500 },
    { date: "周三", new: 12, active: 550 },
    { date: "周四", new: 60, active: 800 },
    { date: "周五", new: 65, active: 550 },
    { date: "周六", new: 53, active: 770 },
    { date: "周日", new: 33, active: 170 }
  ]
};

// 图表配置
const xOptions = reactive({
  textStyle: { color: "#333" },
  legend: {},
  grid: { left: "20%" },
  tooltip: { trigger: "axis" },
  xAxis: {
    type: "category",
    data: [],
    axisLine: { lineStyle: { color: "#17b3a3" } },
    axisLabel: { interval: 0, color: "#333" }
  },
  yAxis: [{
    type: "value",
    axisLine: { lineStyle: { color: "#17b3a3" } }
  }],
  color: ["#2ec7c9", "#b6a2de", "#5ab1ef", "#ffb980", "#d87a80", "#8d98b3"],
  series: []
});

const pieOptions = reactive({
  tooltip: { trigger: "item" },
  legend: {},
  color: ["#0f78f4", "#dd536b", "#9462e5", "#a6a6a6", "#e1bb22", "#39c362", "#3ed1cf"],
  series: []
});

// 渲染图表函数
const renderCharts = () => {
  const { orderData, userData, videoData } = chartData;
  
  // 第一个图表（折线图）
  xOptions.xAxis.data = orderData.date;
  xOptions.series = Object.keys(orderData.data[0]).map(val => ({
    name: val,
    data: orderData.data.map(item => item[val]),
    type: "line"
  }));
  const oneEchart = echarts.init(proxy.$refs["echart"]);
  oneEchart.setOption(xOptions);

  // 第二个图表（柱状图）
  xOptions.xAxis.data = userData.map(item => item.date);
  xOptions.series = [
    { name: "新增用户", data: userData.map(item => item.new), type: "bar" },
    { name: "活跃用户", data: userData.map(item => item.active), type: "bar" }
  ];
  const twoEchart = echarts.init(proxy.$refs["userEchart"]);
  twoEchart.setOption(xOptions);

  // 第三个图表（饼图）
  pieOptions.series = [{ data: videoData, type: "pie" }];
  const threeEchart = echarts.init(proxy.$refs["videoEchart"]);
  threeEchart.setOption(pieOptions);

  // 响应式调整
  observe.value = new ResizeObserver(() => {
    oneEchart.resize();
    twoEchart.resize();
    threeEchart.resize();
  });
  
  if(proxy.$refs["echart"]) {
    observe.value.observe(proxy.$refs["echart"]);
  }
};

onMounted(() => {
  renderCharts();
});

</script>

<style scoped lang="less">
.home {
  height: 100%;
  .user {
    display: flex;
    align-items: center;
    border-bottom: 1px solid #ccc;
    margin-bottom: 20px;
    img {
      width: 150px;
      height: 150px;
      border-radius: 50%;
      margin-right: 40px;
    }
    .user-info {
      p {
        line-height: 40px;
      }
      .user-info-p {
        color: #999;
      }
      .user-info-admin {
        font-size: 35px;
      }
    }
  }
  .login-info {
    p {
      line-height: 30px;
      font-size: 14px;
      color: #999;
      span {
        color: #666;
        margin-left: 60px;
      }
    }
  }
  .num {
    display: flex;
    /* 当容器内的子元素总宽度超过容器宽度时，使用该属性值可让子元素换行显示，以适应容器空间。 */
    flex-wrap: wrap;
    justify-content: space-between;
    .el-card {
      width: 32%;
      margin-bottom: 20px;
    }
    .icons {
      width: 80px;
      height: 80px;
      font-size: 30px;
      text-align: center;
      line-height: 80px;
      color: #fff;
    }
    .detail {
      margin-left: 15px;
      display: flex;
      flex-direction: column;
      justify-content: center;
      .num {
        font-size: 20px;
        margin-bottom: 10px;
      }
      .txt {
        font-size: 14px;
        text-align: center;
        color: #999;
      }
    }
  }
  .graph {
    margin-top: 20px;
    margin-bottom: 20px;
    display: flex;
    justify-content: space-between;
    .el-card {
      width: 48%;
      height: 260px;
    }
  }
}
</style>
